<%#-- copyright
OpenProject is an open source project management software.
Copyright (C) 2012-2020 the OpenProject GmbH

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 3.

OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
Copyright (C) 2006-2017 Jean-Philippe Lang
Copyright (C) 2010-2013 the ChiliProject Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

See docs/COPYRIGHT.rdoc for more details.

++#%>

<%-
  templated ||= false
  index ||= "INDEX"
  new_or_existing = labor_budget_item.new_record? ? 'new' : 'existing'
  id_or_index = labor_budget_item.new_record? ? index : labor_budget_item.id
  prefix = "cost_object[#{new_or_existing}_labor_budget_item_attributes][]"
  id_prefix = "cost_object_#{new_or_existing}_labor_budget_item_attributes_#{id_or_index}"
  name_prefix = "cost_object[#{new_or_existing}_labor_budget_item_attributes][#{id_or_index}]"
  classes ||= ""
  classes += " budget-row-template" if templated

  @labor_budget_item = labor_budget_item
  error_messages = error_messages_for 'labor_budget_item'
-%>

<% unless error_messages.blank? %><tr><td colspan="5"><%= error_messages %></td></tr><% end %>
<%= fields_for prefix, labor_budget_item do |cost_form| %>
  <tr class="cost_entry <%= classes %>" id="<%= id_prefix %>">
    <td class="units">
      <div class="form--field-container">
        <div class="form--text-field-container -xslim">
          <label class="hidden-for-sighted" for="<%= id_prefix %>_units"><%= LaborBudgetItem.human_attribute_name(:hours) %></label>
          <%= cost_form.number_field :hours,
                                     index: id_or_index,
                                     min: 0,
                                     step: 0.01,
                                     class: 'budget-item-value form--text-field',
                                     data: { :'request-key' => 'hours' } %>
        </div>
      </div>
    </td>
    <td class="user">
      <label class="hidden-for-sighted" for="<%= id_prefix %>_user_id"><%= t(:label_user) %></label>
      <%= cost_form.select :user_id,
                           @project.possible_assignees.sort.map{|u| [u.name, u.id]},
                           { prompt: true },
                           {
                             index: id_or_index,
                             class: 'form--select budget-item-value',
                             data: { :'request-key' => 'user_id' }
                           } %>

    </td>
    <td class="comment">
      <label class="hidden-for-sighted" for="<%= id_prefix %>_comments"><%= LaborBudgetItem.human_attribute_name(:comments) %></label>
      <%= cost_form.text_field :comments, index: id_or_index, size: 40 %>
    </td>
    <% if User.current.allowed_to?(:view_cost_rates, @project)%>
      <td class="currency budget-table--fields">
        <%# Keep current budget as hidden field because otherwise they will be overridden %>
        <% if templated == false && !labor_budget_item.new_record? && labor_budget_item.overridden_budget? %>
          <%= cost_form.hidden_field :budget, value: unitless_currency_number(labor_budget_item.budget) %>
        <% end %>

        <% cost_value = labor_budget_item.budget || labor_budget_item.calculated_costs(@cost_object.fixed_date, @cost_object.project_id) %>
        <%= cost_form.hidden_field :currency, index: id_or_index, value: Setting.plugin_openproject_costs['costs_currency'] %>
        <cost-unit-subform obj-id="<%= id_prefix %>"
                           obj-name="<%= "#{name_prefix}[budget]" %>">
          <a id="<%= "#{id_prefix}_costs" %>" class="costs--edit-planned-costs-btn icon-context icon-edit" title="<%= t(:help_click_to_edit) %>">
            <% if labor_budget_item.costs_visible_by?(User.current) %>
              <%= cost_form.hidden_field :cost_value, index: id_or_index, value: unitless_currency_number(cost_value) %>
              <%= number_to_currency(cost_value) %>
            <% end %>
          </a>
        </cost-unit-subform>
      </td>
    <% end %>
    <td class="delete budget-table--fields buttons">
      <a class="delete-budget-item no-decoration-on-hover" title="<%= t(:button_delete) %>" href="#">
        <%= op_icon('icon-context icon-delete') %>
        <span class="hidden-for-sighted"><%= t(:button_delete) %></span>
      </a>
    </td>
  </tr>
<% end %>
